package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.util.CategoryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryUtil categoryUtil;

    @SuppressWarnings("ALL")
    @Override
    public PageUtils queryPage(Map<String, Object> params, String type, Long catalogId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        //base:1 基本信息 sale:0 销售属性
        if (type.equals("base")) {
            wrapper.eq("attr_type",1);
        } else {
            wrapper.eq("attr_type",0);
        }

        if (!catalogId.equals(0L)) {
            wrapper.eq("catalog_id",catalogId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> attrRelationByGroupId(Long attrGroupId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type",1);
        wrapper.eq("attr_group_id",attrGroupId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public PageUtils noAttrRelation(Map<String, Object> params) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.isNull("attr_group_id");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public boolean save(AttrEntity entity) {
        Long attrGroupId = entity.getAttrGroupId();
        Long catalogId = entity.getCatalogId();

        if (attrGroupId != null) {
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
            entity.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        if (catalogId != null) {
            CategoryEntity categoryEntity = categoryDao.selectById(catalogId);
            entity.setCatalogName(categoryEntity.getName());
        }

        return super.save(entity);
    }

    @Override
    public AttrEntity getById(Serializable id) {
        AttrEntity attrEntity = baseMapper.selectById(id);
        Long catalogId = attrEntity.getCatalogId();
        Long[] catalogIds = categoryUtil.getCatalogIds(catalogId);
        attrEntity.setCatalogPath(catalogIds);
        return attrEntity;
    }
}